﻿/**
 * ngArea - AngularJS module for area.
 *
 *
 * Credits
 * =======
 *
 * Copyright 2016 LonghronShen <LonghronShen@Hotmail.com>
 */

(function () {
    "use strict";

    /**
     * Config
     */
    var moduleName = 'angularUtils.directives.ngArea';
    var DEFAULT_ID = '__default';

    /**
     * Module
     */
    var module;
    try {
        module = angular.module(moduleName);
    } catch (err) {
        // named module does not exist, so create one
        module = angular.module(moduleName, []);
    }

    module.directive('ngArea', ['$rootScope', function ($rootScope) {
        return {
            restrict: 'E',
            transclude: true,
            scope: {
                province: '=',
                city: '=',
                area: '=',
                cityList: '=',
                areaList: '='
            },
            controller: ["$scope", "$q", "dataService", "$ngBootbox", function ($scope, $q, dataService, $ngBootbox) {

                $scope.provinceList = "";
                $scope.cityList = "";
                $scope.areaList = "";

                $scope.province = $scope.province || "";
                $scope.city = $scope.city || "";
                $scope.area = $scope.area || "";

                dataService.Area().then(function (res) {
                    $scope.provinceList = res.list
                }, function (rej) {
                    $ngBootbox.alert(rej)
                });


                $scope.changeProvince = function () {
                    if ($scope.province === null || $scope.province == "" || $scope.province == undefined) {
                        $scope.province = -1;
                        $scope.area = -1;
                        $scope.areaList = "";
                    }
                    dataService.provincecode($scope.province).then(function (res) {
                        $scope.cityList = res.list;
                        $scope.areaList = "";
                    }, function (rej) {
                        console.error("dataService.provincecode error: ", rej);
                    });
                };

                $scope.changeCity = function () {
                    if ($scope.city === "null" || $scope.city == "" || $scope.city == undefined) {
                        $scope.city = -1;
                        $scope.area = -1;
                        $scope.areaList = "";
                    }
                    dataService.cityCode($scope.city).then(function (res) {
                        $scope.areaList = res.list;
                    }, function (res) {
                        //$ngBootbox.alert(res);
                        console.error("dataService.cityCode error: ", rej);
                    });
                };





                //$scope.changeProvince();
                //$scope.changeCity();

                //$scope.$watch(function () { return $scope.province; }, function (newValue, oldValue) {
                //    console.info("<area.$watch_province>: province changed.", newValue);
                //    $rootScope.$broadcast('AREA:PROVINCE_CHANGED', $scope);
                //});
                //$scope.$watch(function () { return $scope.city; }, function (newValue, oldValue) {
                //    console.info("<area.$watch_city>: city changed.", newValue);
                //    $rootScope.$broadcast('AREA:CITY_CHANGED', $scope);
                //});
                //$scope.$watch(function () { return $scope.area; }, function (newValue, oldValue) {
                //    console.info("<area.$watch_area>: area changed.", newValue);
                //    $rootScope.$broadcast('AREA:AREA_CHANGED', $scope);
                //});
            }],
            link: function (scope, elem, attrs) {
                // You can place any pre-initialization code here.
                // For example, through the 'elem' object you can change the DOM elements in a jQuery way according to the data which is presented by 'attrs'.
            },
            templateUrl: '/js/directives/ng-area.html',
            replace: true
        };
    }]);
})();